package database;

import java.sql.*;
import java.util.ArrayList;

public class DataBase {
    private static ArrayList<String> addresses = new ArrayList<>();
    private static ArrayList<String> stopWords = new ArrayList<>();
    private static Connection conn;

    public static void init() throws Exception {
        if (conn == null) {
            // Connect to SQLite's JDBC
            Class.forName("org.sqlite.JDBC");

            // Create a connection with the database.
            conn = DriverManager.getConnection("jdbc:sqlite:database/homework_4.db");
        }
    }

    public static ArrayList<String> getAddresses() {
        try {
            init();
            Statement stat = conn.createStatement();

            // Attain all of email addresses in the addresses table.
            ResultSet rs = stat.executeQuery("select * from addresses;");
            // Store the results.
            while (rs.next()) {
                addresses.add(rs.getString("addr"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addresses;
    }

    public static ArrayList<String> getStopWords() {
        try {
            init();
            Statement stat = conn.createStatement();

            // Attain stopwords in the table.
            ResultSet rs = stat.executeQuery("select * from stopwords;");

            // Store the results.
            while (rs.next()) {
                stopWords.add(rs.getString("word"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stopWords;
    }
}
